//
//  FQDMenuItemCell.m
//  FakeQDaily
//
//  Created by mewxiv on 8/28/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMenuItemCell.h"
#import "FQDMenuItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDMenuItemCell ()

// 图标按钮
@property (nonatomic, strong) UIImageView *iconImageView;
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 显示更多的箭头
@property (nonatomic, strong) UIImageView *arrowImageView;

@end

@implementation FQDMenuItemCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    UIImage *iconImage = [UIImage imageNamed:@"menu_category"];
    _iconImageView = [[UIImageView alloc] initWithImage:iconImage];
    [self addSubview:_iconImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDRegularFontWithSize(16.0f);
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text = @"大公司头条";
    [self addSubview:_titleLabel];
    
    UIImage *arrowImage = [UIImage imageNamed:@"sidebar_next"];
    _arrowImageView = [[UIImageView alloc] initWithImage:arrowImage];
    [self addSubview:_arrowImageView];
    _arrowImageView.hidden = YES;
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf);
        make.left.equalTo(weakSelf);
        make.width.equalTo(weakSelf.mas_height);
        make.height.equalTo(weakSelf.mas_height);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf);
        make.left.equalTo(weakSelf.iconImageView.mas_right).offset(10);
        make.right.lessThanOrEqualTo(weakSelf.mas_right);
        make.height.equalTo(weakSelf.mas_height);
    }];
    
    [_arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLabel.mas_right).offset(10);
        make.centerY.equalTo(weakSelf);
        make.size.mas_equalTo(CGSizeMake(10.0f, 18.0f));
    }];
}

- (void)setMenuItem:(FQDMenuItem *)menuItem
{
    _menuItem = menuItem;
    
    if (menuItem.iconName) {
        UIImage *image = [UIImage imageNamed:menuItem.iconName];
        self.iconImageView.image = image;
    } else {
        NSURL *url = [NSURL URLWithString:menuItem.iconURL];
        [self.iconImageView sd_setImageWithURL:url];
    }
    
    self.titleLabel.text = menuItem.title;
    
    BOOL isContainer = [menuItem isKindOfClass:[FQDMenuItemContainer class]];
    self.arrowImageView.hidden = !isContainer;
}

- (CGRect)touchableRect
{
    CGRect iconImageRect = self.iconImageView.frame;
    CGRect titleLabelRect = self.titleLabel.frame;
    return CGRectUnion(iconImageRect, titleLabelRect);
}

+ (NSString *)reuseId
{
    return @"FQDMenuItemCellId";
}

@end
